package cn.com.miao.lynx.service.system.service.impl;

import cn.com.miao.lynx.common.base.repository.BaseRepository;
import cn.com.miao.lynx.service.system.entity.QSysPermission;
import cn.com.miao.lynx.service.system.entity.QSysRolePermission;
import cn.com.miao.lynx.service.system.entity.QSysUserRole;
import cn.com.miao.lynx.service.system.entity.SysPermission;
import cn.com.miao.lynx.service.system.repository.SysPermissionRepository;
import cn.com.miao.lynx.service.system.service.SysPermissionService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * SysPermissionServiceImpl
 *
 * @title: SysPermissionServiceImpl
 * @description:
 * @author: dengmiao
 * @create: 2020-01-06 15:10
 **/
@Slf4j
@Data
@AllArgsConstructor
@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    private final SysPermissionRepository sysPermissionRepository;

    @Override
    public BaseRepository<SysPermission, Long> getRepository() {
        return this.getSysPermissionRepository();
    }

    @Override
    public List<SysPermission> findPermissionListByUserId(Long userId) {
        // 权限 queryDsl查询
        QSysPermission _Q_SysPermission = QSysPermission.sysPermission;
        QSysRolePermission _Q_SysRolePermission = QSysRolePermission.sysRolePermission;
        QSysUserRole _Q_SysUserRole = QSysUserRole.sysUserRole;
        List<SysPermission> permissions = jpaQueryFactory().select(_Q_SysPermission)
                .from(_Q_SysPermission, _Q_SysRolePermission, _Q_SysUserRole)
                .where(
                        // 关联查询
                        _Q_SysPermission.id.eq(_Q_SysRolePermission.permissionId),
                        _Q_SysRolePermission.roleId.eq(_Q_SysUserRole.roleId),
                        _Q_SysUserRole.userId.eq(userId)
                )
                // 排序
                .orderBy(_Q_SysPermission.sortNo.asc())
                // 执行查询
                .fetch();
        return permissions;
    }
}
